package com.tao.utilslib.log.logsave;

import com.tao.utilslib.data.Obj;

import java.util.HashMap;
import java.util.Map;

public class LogSaveManager {
    static LogSaveManager logManager = new LogSaveManager();

    private LogSaveManager() {
    }

    public static LogSaveManager instance() {
        return logManager;
    }

    Map<String, ILogSeaveer> logerMap = new HashMap<>();

    public ILogSeaveer logServer(String path) {
        return logServer(path, LogSaveType.addHead);
    }

    public ILogSeaveer logServer(String path, long logFileSize) {
        return logServer(path, LogSaveType.addHead, logFileSize);
    }

    public ILogSeaveer logServer(String path, LogSaveType type) {
        ILogSeaveer logSaveer = logerMap.get(path);
        if (Obj.isNULL(logSaveer)) {
            logSaveer = createDefaultSaveer(path, type);
            logerMap.put(path, logSaveer);
        }
        return logSaveer;
    }

    public ILogSeaveer logServer(String path, LogSaveType type, long logFileSize) {
        ILogSeaveer logSaveer = logServer(path, type);
        if (logSaveer instanceof BaseLogSaveer) {
            ((BaseLogSaveer) logSaveer).setMaxlog(logFileSize);
        }
        return logSaveer;
    }

    private BaseLogSaveer createDefaultSaveer(String path, LogSaveType type) {

        switch (type) {
            case replace:
                return new RepleaseSaveer(path);
            case addEnd:
                return new AddEndSaveer(path);
            case addHead:
                return new AddHeadSaveer(path);
        }
        return new RepleaseSaveer(path);
    }


    public static void main(String[] args) {
        ILogSeaveer logServer = instance().logServer("C:\\Users\\Administrator\\Desktop\\log1.txt");
        ILogSeaveer logServer1 = instance().logServer("C:\\Users\\Administrator\\Desktop\\log2.txt", LogSaveType.replace);
        ILogSeaveer logServer2 = instance().logServer("C:\\Users\\Administrator\\Desktop\\log3.txt", LogSaveType.addHead);

        for (int i = 0; i < 1000; i++) {
//      logServer.log("logger:"+i+"_"+System.currentTimeMillis());
//      logServer1.log("logger:"+i+"_"+System.currentTimeMillis());
            logServer2.log("logger:" + i + "_" + System.currentTimeMillis());
        }

    }


}
